package cn.mzcode.xyzpfuwuduan.service.impl;

import cn.mzcode.xyzpfuwuduan.common.ServerResponse;
import cn.mzcode.xyzpfuwuduan.dao.CompanyMapper;
import cn.mzcode.xyzpfuwuduan.dao.PositionMapper;
import cn.mzcode.xyzpfuwuduan.pojo.Company;
import cn.mzcode.xyzpfuwuduan.pojo.Position;
import cn.mzcode.xyzpfuwuduan.service.ICompanyService;
import cn.mzcode.xyzpfuwuduan.vo.CompanyVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.ldap.PagedResultsControl;
import java.util.List;

@Service
public class CompanyServiceImpl implements ICompanyService {

    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private PositionMapper positionMapper;


    @Override
    public ServerResponse saveOrUpdateCompany(Company company) {
        if (company!=null) {
            if (company.getId() != null) {
                int rowCount = companyMapper.updateByPrimaryKey(company);
                if (rowCount > 0) {
                    return ServerResponse.createBySuccess("更新公司成功");
                }
                return ServerResponse.createByErrorMessage("更新公司失败");
            } else {
                int rowCount = companyMapper.insert(company);
                if (rowCount > 0) {
                    return ServerResponse.createBySuccess("添加公司成功");
                }
                return ServerResponse.createByErrorMessage("添加公司失败");
            }
        }
        return ServerResponse.createByErrorMessage("新增或更新公司参数不正确");
    }

    @Override
    public ServerResponse<PageInfo> selectCompany(Company company, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
//        List<Company> companyList=companyMapper.selectBySelective(company);
//        PageInfo<CompanyVo> pageInfo = new PageInfo(companyList);
//        return ServerResponse.createBySuccess(pageInfo);

        List<Company> companyList=companyMapper.selectBySelective(company);
        List<CompanyVo> companyVoList=Lists.newArrayList();
        for (Company companyItem:companyList){
            int positionId=companyItem.getId();
            Position position=positionMapper.selectByPrimaryKey(positionId);
            CompanyVo companyVo=assembleCompanyList(companyItem,position);
            companyVoList.add(companyVo);
        }
        PageInfo<CompanyVo> pageInfo = new PageInfo(companyList);
        pageInfo.setList(companyVoList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    private CompanyVo assembleCompanyList(Company companyItem, Position position) {
        CompanyVo companyVo=new CompanyVo();
        companyVo.setId(companyItem.getId());
        companyVo.setCompanyName(companyItem.getCompanyName());
        companyVo.setLocation(companyItem.getLocation());
        companyVo.setCompanyType(companyItem.getCompanyType());
        companyVo.setCompanyScale(companyItem.getCompanyScale());
        companyVo.setFacing(companyItem.getFacing());
        companyVo.setIsrecruitment(companyItem.getIsrecruitment());
        companyVo.setStatus(companyItem.getStatus());
        companyVo.setDetail(companyItem.getDetail());
        if (position!=null) {
            companyVo.setPositionName(position.getPositionName());
            companyVo.setSalary(position.getSalary());
            companyVo.setPostitonNumber(position.getPositionNumber());
            companyVo.setWorkplace(position.getWorkplace());
            companyVo.setTypes(position.getTypes());
            companyVo.setTimes(position.getTimes());
            companyVo.setRecruitmentType(position.getRecruitmentType());
            companyVo.setRecruitmentLocation(position.getRecruitmentLocation());
            companyVo.setContact(position.getContact());
            companyVo.setContactPhone(position.getContactPhone());
            companyVo.setEmail(position.getEmail());
            companyVo.setFroms(position.getFroms());
            companyVo.setDescribeAndRequire(position.getDescribeAndRequire());
        }
        return companyVo;
    }


}
